<?php

/**
 * 首页达人新增AJAX
 * 
 * @copyright zhanghuan
 * @since 2012-03-23
 */

include_once($_SERVER["DOCUMENT_ROOT"] . "/core/inc.php");
include_once($_SERVER["DOCUMENT_ROOT"] . "/core/admin.inc.php");
include_once($_SERVER["DOCUMENT_ROOT"] . "/include/foodv2/index/db_food_user_daren_index.class.php");
include_once($_SERVER["DOCUMENT_ROOT"] . "/include/foodv2/inc.php");

$g_cgival = array();
$g_pro = array();
$g_show = array();


//$_GET["db_debug"] = 1;

function check_cgi_pro()
{
	global $g_cgival, $g_pro, $g_show;
	
	if (!security_check::check_refer())
	{
		throw new Exception("错误的来路", 1);
	}
	
	//id
	cgi::both($g_cgival["id"], "id", "");
	if (!cgi_chk::uid($g_cgival["id"]) && !empty($g_cgival["id"]))
	{
		throw new Exception("参数id错误");
	}
	//uid
	cgi::both($g_cgival["uid"], "uid", "");
	if (!cgi_chk::uid($g_cgival["uid"]))
	{
		throw new Exception("参数uid错误");
	}
	//前台位置
	cgi::both($g_cgival["pos"], "pos", "");
	if (!intval($g_cgival["pos"]))
	{
		throw new Exception("参数pos错误");
	}
	//推荐理由
	cgi::both($g_cgival["desc"], "desc", "");
	$g_cgival["desc"] = string::un_script_code($g_cgival["desc"]);
	$g_cgival["desc"] = string::un_html($g_cgival["desc"]);
}

function get_data()
{
	global $g_cgival, $g_pro, $g_show;
	
	$id = $g_cgival["id"];
	$uid = $g_cgival["uid"];
	$desc = $g_cgival["desc"];
	$pos = $g_cgival["pos"];
	
	$db_user = new db_food_user();
	$db_daren = new db_food_user_daren_index();
	$admin_log = new admin_log();
	
	$site_id = admin::admin_get_user_site_id();	// 拿到当前站点的site_id
	$admin_id = admin::admin_get_login_uid();	// 拿到当前管理员ID
	$admin_name = admin::admin_get_login_user_name(); // 拿到当前管理员名称
	
	// 根据达人uid获取达人属性
	$mod_food_user = new mod_food_user();
	$user = $mod_food_user->get_food_user_info($uid);
	if ($user["error"] == 1)
	{
		throw new Exception("此uid无效");
	}
	
	if ($_FILES['file1'] and $_FILES["file1"]["tmp_name"] != "")
	{
		try
		{
			/* 上传图片 */
			$upload = new s3_upload();
			$filename = "index_user_daren/".time();
			
			if ($pos == 1)
			{
				$data = $upload->show($_FILES['file1'])->zomm("330,283")->save($filename);
			}
			else
			{
				$data = $upload->show($_FILES['file1'])->zomm("138,138")->save($filename);
			} 
			
			if (is_array($data))
			{
				$img_src = $data["src"];
			}
		}
		catch (Exception $e)
		{
			throw new Exception($e->getMessage(), 1);
		}
	}
	
	// 编辑
	if (!empty($id))
	{
		$res = $db_daren->admin_update_item_by_id(
				$id,
				$uid,
				$site_id,
				$user["nickname"],
				$img_src,
				$desc,
				$pos,
				$admin_id, 
				$admin_name
		);
		if(!$res)
		{
			throw new Exception("编辑失败");
		}
		$admin_log->admin_insert_log_info($admin_id, $site_id, 12, "编辑 首页达人uid={$id}");
	}
	// 新建首页推荐达人
	else
	{
		$res_exists = $db_daren->admin_get_item_by_uid($uid, $site_id);
		if(!empty($res_exists['uid']))
		{
			throw new Exception("当前站点已经推荐过这个用户");
		}
		else 
		{
			$res = $db_daren->admin_insert_data(
					$uid,
					$site_id,
					$user["nickname"],
					$img_src,
					$desc,
					$pos,
					$admin_id,
					$admin_name
			);
		}
		if(!$res)
		{
			throw new Exception("添加失败");
		}
		$admin_log->admin_insert_log_info($admin_id, $site_id, 12, "新增 首页达人uid={$uid}");
	}
}

try
{
	admin_check_login();
	
	check_cgi_pro();
	get_data();
	
	$g_show["error"] = 0;
	$g_show["errmsg"] = '';
}
catch(Exception $e)
{
	$g_show["error"] = 1;
	$g_show["errmsg"] = $e->getMessage();
}
show_pro();
exit;

function show_pro()
{
	global $g_cgival, $g_pro, $g_show;
	if(isset($_GET['debug']))
		print_r($g_show);
	//echo output::json('json',$g_show);
	echo json_encode($g_show);
}
?>